package com.itfitness.mchart.listener;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.hyphenate.EMContactListener;
import com.hyphenate.EMGroupChangeListener;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMucSharedFile;
import com.itfitness.mchart.bean.InviteNotifyBean;
import com.itfitness.mchart.constant.Constant;
import com.itfitness.mchart.model.dao.InviteNotifyDao;
import com.itfitness.mchart.model.db.DatabaseHelper;

import java.util.List;

/**
 * @ProjectName: MChart
 * @Package: com.itfitness.mchart.listener
 * @ClassName: InviteNotifyListener
 * @Description: java类作用描述 ：全局的邀请通知监听
 * @Author: 作者名：lml
 * @CreateDate: 2019/4/25 13:53
 * @UpdateUser: 更新者：
 * @UpdateDate: 2019/4/25 13:53
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */

public class InviteNotifyListener {
    private LocalBroadcastManager localBroadcastManager;
    private static InviteNotifyListener instance;
    private Context context;
    private static String mCurrentuserName;
    private InviteNotifyListener(Context context) {
        localBroadcastManager = LocalBroadcastManager.getInstance(context);
        this.context = context;
    }
    public static synchronized InviteNotifyListener getInstance(Context context) {
        if (instance == null) {
            synchronized (DatabaseHelper.class) {
                if (instance == null) {
                    instance = new InviteNotifyListener(context);
                }
            }
        }
        mCurrentuserName = EMClient.getInstance().getCurrentUser();
        return instance;
    }
    /**
     * 开始监听
     */
    public void listen() {
        listenContact();//监听好友信息
        listenGroup();//监听群组信息
    }

    /**
     * 监听群组通知
     */
    private void listenGroup() {
        EMClient.getInstance().groupManager().addGroupChangeListener(new EMGroupChangeListener() {
            @Override
            public void onInvitationReceived(String groupId, String groupName, String inviter, String reason) {
                //接收到群组加入邀请
                InviteNotifyDao inviteNotifyDao = new InviteNotifyDao(context);
                inviteNotifyDao.update(new InviteNotifyBean(InviteNotifyBean.INVITETYPE_GROUP,mCurrentuserName,groupId,groupName,inviter,InviteNotifyBean.InviteNotifyStatus.GROUP_INVITATIONRECEIVED));
                SPUtils.getInstance().put(Constant.SP_FLAGINVITENOTIFY,true);
                localBroadcastManager.sendBroadcast(new Intent(Constant.ACTION_INVITENOTIFY));
            }

            @Override
            public void onRequestToJoinReceived(String groupId, String groupName, String applyer, String reason) {
                //用户申请加入群
                InviteNotifyDao inviteNotifyDao = new InviteNotifyDao(context);
                inviteNotifyDao.update(new InviteNotifyBean(InviteNotifyBean.INVITETYPE_GROUP,mCurrentuserName,groupId,groupName,applyer,InviteNotifyBean.InviteNotifyStatus.GROUP_APPLYJOIN));
                SPUtils.getInstance().put(Constant.SP_FLAGINVITENOTIFY,true);
                localBroadcastManager.sendBroadcast(new Intent(Constant.ACTION_INVITENOTIFY));
            }

            @Override
            public void onRequestToJoinAccepted(String groupId, String groupName, String accepter) {
                //同意加群申请
                InviteNotifyDao inviteNotifyDao = new InviteNotifyDao(context);
                inviteNotifyDao.update(new InviteNotifyBean(InviteNotifyBean.INVITETYPE_GROUP,mCurrentuserName,groupId,groupName,accepter,InviteNotifyBean.InviteNotifyStatus.GROUP_ACCEPTEDINVITATION));
                SPUtils.getInstance().put(Constant.SP_FLAGINVITENOTIFY,true);
                localBroadcastManager.sendBroadcast(new Intent(Constant.ACTION_INVITENOTIFY));
            }

            @Override
            public void onRequestToJoinDeclined(String groupId, String groupName, String decliner, String reason) {
                //拒绝加群申请
                InviteNotifyDao inviteNotifyDao = new InviteNotifyDao(context);
                inviteNotifyDao.update(new InviteNotifyBean(InviteNotifyBean.INVITETYPE_GROUP,mCurrentuserName,groupId,groupName,decliner,InviteNotifyBean.InviteNotifyStatus.GROUP_DECLINEDINVITATION));
                SPUtils.getInstance().put(Constant.SP_FLAGINVITENOTIFY,true);
                localBroadcastManager.sendBroadcast(new Intent(Constant.ACTION_INVITENOTIFY));
            }

            @Override
            public void onInvitationAccepted(String groupId, String inviter, String reason) {
                //群组邀请被同意
                InviteNotifyDao inviteNotifyDao = new InviteNotifyDao(context);
                inviteNotifyDao.update(new InviteNotifyBean(InviteNotifyBean.INVITETYPE_GROUP,mCurrentuserName,groupId,"",inviter,InviteNotifyBean.InviteNotifyStatus.GROUP_INVITATIONACCEPTED));
                SPUtils.getInstance().put(Constant.SP_FLAGINVITENOTIFY,true);
                localBroadcastManager.sendBroadcast(new Intent(Constant.ACTION_INVITENOTIFY));
            }

            @Override
            public void onInvitationDeclined(String groupId, String invitee, String reason) {
                //群组邀请被拒绝
                InviteNotifyDao inviteNotifyDao = new InviteNotifyDao(context);
                inviteNotifyDao.update(new InviteNotifyBean(InviteNotifyBean.INVITETYPE_GROUP,mCurrentuserName,groupId,"",invitee,InviteNotifyBean.InviteNotifyStatus.GROUP_INVITATIONDECLINED));
                SPUtils.getInstance().put(Constant.SP_FLAGINVITENOTIFY,true);
                localBroadcastManager.sendBroadcast(new Intent(Constant.ACTION_INVITENOTIFY));

            }

            @Override
            public void onUserRemoved(String groupId, String groupName) {
                //用户被移除
            }

            @Override
            public void onGroupDestroyed(String groupId, String groupName) {
                //群组被解散
            }

            @Override
            public void onAutoAcceptInvitationFromGroup(String groupId, String inviter, String inviteMessage) {
                //接收邀请时自动加入到群组的通知
                InviteNotifyDao inviteNotifyDao = new InviteNotifyDao(context);
                inviteNotifyDao.update(new InviteNotifyBean(InviteNotifyBean.INVITETYPE_GROUP,mCurrentuserName,groupId,"",inviter,InviteNotifyBean.InviteNotifyStatus.GROUP_AUTOACCEPTINVITATIONFROMGROUP));
                SPUtils.getInstance().put(Constant.SP_FLAGINVITENOTIFY,true);
                localBroadcastManager.sendBroadcast(new Intent(Constant.ACTION_INVITENOTIFY));
            }

            @Override
            public void onMuteListAdded(String groupId, final List<String> mutes, final long muteExpire) {
                //成员禁言的通知
            }

            @Override
            public void onMuteListRemoved(String groupId, final List<String> mutes) {
                //成员从禁言列表里移除通知
            }

            @Override
            public void onAdminAdded(String groupId, String administrator) {
                //增加管理员的通知
            }

            @Override
            public void onAdminRemoved(String groupId, String administrator) {
                //管理员移除的通知
            }

            @Override
            public void onOwnerChanged(String groupId, String newOwner, String oldOwner) {
                //群所有者变动通知
            }
            @Override
            public void onMemberJoined(final String groupId,  final String member){
                //群组加入新成员通知
            }
            @Override
            public void onMemberExited(final String groupId, final String member) {
                //群成员退出通知
            }

            @Override
            public void onAnnouncementChanged(String groupId, String announcement) {
                //群公告变动通知
            }

            @Override
            public void onSharedFileAdded(String groupId, EMMucSharedFile sharedFile) {
                //增加共享文件的通知
            }

            @Override
            public void onSharedFileDeleted(String groupId, String fileId) {
                //群共享文件删除通知
            }
        });

    }

    /**
     * 监听好友通知
     */
    private void listenContact() {
        EMClient.getInstance().contactManager().setContactListener(new EMContactListener() {

            @Override
            public void onContactInvited(String username, String reason) {
                //收到好友邀请
                    InviteNotifyDao inviteNotifyDao = new InviteNotifyDao(context);
                    inviteNotifyDao.update(new InviteNotifyBean(InviteNotifyBean.INVITETYPE_CONTACT,mCurrentuserName,"","",username,InviteNotifyBean.InviteNotifyStatus.CONTACT_CONTACTINVITED));
                    SPUtils.getInstance().put(Constant.SP_FLAGINVITENOTIFY,true);
                    localBroadcastManager.sendBroadcast(new Intent(Constant.ACTION_INVITENOTIFY));
            }

            @Override
            public void onFriendRequestAccepted(String username) {
                //好友请求被同意
                InviteNotifyDao inviteNotifyDao = new InviteNotifyDao(context);
                inviteNotifyDao.update(new InviteNotifyBean(InviteNotifyBean.INVITETYPE_CONTACT,mCurrentuserName,"","",username,InviteNotifyBean.InviteNotifyStatus.CONTACT_FRIENDREQUESTACCEPTED));
                SPUtils.getInstance().put(Constant.SP_FLAGINVITENOTIFY,true);
                localBroadcastManager.sendBroadcast(new Intent(Constant.ACTION_INVITENOTIFY));
            }

            @Override
            public void onFriendRequestDeclined(String username) {
                //好友请求被拒绝
                InviteNotifyDao inviteNotifyDao = new InviteNotifyDao(context);
                inviteNotifyDao.update(new InviteNotifyBean(InviteNotifyBean.INVITETYPE_CONTACT,mCurrentuserName,"","",username,InviteNotifyBean.InviteNotifyStatus.CONTACT_FRIENDREQUESTDECLINED));
                SPUtils.getInstance().put(Constant.SP_FLAGINVITENOTIFY,true);
                localBroadcastManager.sendBroadcast(new Intent(Constant.ACTION_INVITENOTIFY));
            }

            @Override
            public void onContactDeleted(String username) {
                //被删除时回调此方法
                localBroadcastManager.sendBroadcast(new Intent(Constant.ACTION_CONTACTCHANGE));
            }
            @Override
            public void onContactAdded(String username) {
                //增加了联系人时回调此方法
                localBroadcastManager.sendBroadcast(new Intent(Constant.ACTION_CONTACTCHANGE));
            }
        });

    }
}
